package com.inlycat.utils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Random;
import java.util.UUID;

/**
 * @author jarvan4dev@163.com
 * @since 2015年8月18日下午2:36:55
 * @category 字符串操作工具
 **/
public class StrUtils implements Runnable{

	/**
	 * MD5加密
	 * 
	 * @param source
	 *            要加密的数据
	 * @return 加密后的数据
	 * @throws NoSuchAlgorithmException
	 * @throws UnsupportedEncodingException
	 */
	public static String MD5Encryption(String source) {
		MessageDigest digest = null;
		try {
			digest = MessageDigest.getInstance("MD5");
			digest.reset();
			digest.update(source.getBytes("UTF8"));
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		byte[] result = digest.digest();
		StringBuffer strBuffer = new StringBuffer();
		for (int i = 0; i < result.length; i++) {
			if (Integer.toHexString(0xFF & result[i]).length() == 1) {
				strBuffer.append("0").append(
						Integer.toHexString(0xFF & result[i]));
			} else {
				strBuffer.append(Integer.toHexString(0xFF & result[i]));
			}
		}
		return strBuffer.toString();
	}

	/**
	 * 生成随机四位数
	 * 
	 * @return
	 */
	public static int getSMSCode() {
		Random random = new Random();// 实例化一个random的对象ne
		int result = random.nextInt(9999 - 1000 + 1) + 1000;// 为变量赋随机值1000-9999
		return result;
	}

	/**
	 * 产生短长度的8位长的文件名
	 * 
	 * @return
	 */
	public static String getFileName() {

		String[] chars = new String[] { "a", "b", "c", "d", "e", "f", "g", "h",
				"i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
				"u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
				"6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H",
				"I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
				"U", "V", "W", "X", "Y", "Z" };

		StringBuffer fileName = new StringBuffer();
		String uuid = UUID.randomUUID().toString();
		uuid = uuid.replaceAll("\\-", "");
		for (int i = 0; i < 8; i++) {
			String code = uuid.substring(i * 4, (i + 1) * 4);
			/**
			 * 16进制转10进制
			 */
			int x = Integer.parseInt(code, 16);
			fileName.append(chars[x % 0x3E]);
		}
		return fileName.toString();
	}

	public static void main(String[] args){
		
		/*Thread thread = null;
		for(int i=0;i<100;i++){
			thread = new Thread(new StrUtils());
			thread.start();
		}*/
		System.out.println(StrUtils.MD5Encryption("12345678"));
		
	}

	@Override
	public void run() {
		Connection connection = null;
		PreparedStatement statement = null;
		try {
			Class.forName("org.mariadb.jdbc.Driver");
			connection = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/test", "root", "123456");
			for (int i = 0; i < 1000000; i++) {
				String sql = "insert into tb_user values (?)";
				statement = connection.prepareStatement(sql);
				statement.setString(1, getFileName());
				statement.execute();
			}
			connection.close();
		} catch (SQLException | ClassNotFoundException e) {
			e.printStackTrace();
		}
		test();
	}
	
	private void test(String... objects){
		
	}
}
